//package cn.maidouya.hrm.filter;
//import com.alibaba.fastjson.JSON;
//import com.netflix.zuul.ZuulFilter;
//import com.netflix.zuul.context.RequestContext;
//import com.netflix.zuul.exception.ZuulException;
//import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.GrantedAuthority;
//import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.security.oauth2.provider.OAuth2Authentication;
//import org.springframework.stereotype.Component;
//
//import java.util.*;
//
//@Component
//public class AuthenZuulFilter  extends ZuulFilter{
//    @Override
//    public String filterType() {
//        return FilterConstants.PRE_TYPE; // pre先执行
//    }
//
//    @Override
//    public int filterOrder() {
//        return 0; // 数字越小优先执行
//    }
//
//    @Override
//    public boolean shouldFilter() {
//        return true; // 开启注解支持
//    }
//    /**
//     * 1. zuul配置文件放行配置
//     *
//     */
//
//    @Override
//    public Object run() throws ZuulException {
//        // 1. 获取用户对象
//        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//        // 2. 判断不为oauth2类型范围null
//        if (!(authentication instanceof OAuth2Authentication)){
//            return null;
//        }
//        // 3.
//        Object principal = authentication.getPrincipal();
//        Map<String, String> requestParameters = ((OAuth2Authentication) authentication).getOAuth2Request().getRequestParameters();
//        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
//        List<String> authoritiesList = new ArrayList<>(authorities.size());
//        authorities.stream().forEach(auth->{
//            authoritiesList.add(auth.getAuthority());
//        });
//
//
//        Map<String, Object> map = new HashMap<>(requestParameters);
//        map.put("principal",principal);
//        map.put("authorities",authoritiesList);
//
//        String jsonToken = JSON.toJSONString(map);
//        System.out.println("网关token处理：" + jsonToken);
//        RequestContext.getCurrentContext().addZuulRequestHeader("token",jsonToken);
//
//
//
//        return null;
//    }
//}